package com.chunuo.mvc.controller;

import com.alibaba.druid.sql.visitor.functions.Char;
import com.chunuo.configuration.annotation.*;
import com.chunuo.configuration.constant.RequestMethod;
import com.chunuo.mvc.common.R;
import com.chunuo.mvc.entity.SysUser;
import com.chunuo.mvc.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Bool;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-12-22 13:30
 */
@RequestMapping("user")
@Component
public class UserController {

    @Autowired
    public UserService userService;


    @RequestMapping(value = "list/{id}/{page}/{limit}",method = RequestMethod.GET)
    public R list(@PathVariable("id")String id,@PathVariable("page")Integer page,@PathVariable("limit") Integer limit){
        System.out.println(id+" = "+page+" = "+limit);
        List<SysUser> sysUserList = userService.findSysUserList();
        return R.ok().data("list",sysUserList);
    }

    @RequestMapping(value = "info",method = RequestMethod.GET)
    public R info(@RequestParam("userId") String id){
        SysUser user = userService.findSysUserById(id);
        return R.ok().data("info",user);
    }

    @RequestMapping(value = "save",method = RequestMethod.POST)
    public R save(@RequestBody SysUser sysUser){
        System.out.println("保存数据"+sysUser);
        userService.saveOrUpdate(sysUser);
        return R.ok().message("保存成功");
    }

    @RequestMapping(value = "delete/{id}",method = RequestMethod.DELETE)
    public R delete(@RequestParam("username")String username,@PathVariable("id")String id){
        System.out.println("删除 > "+username+":"+id);
        int count = userService.deleteSysUserByUsername(null,id);
        return R.ok().message("删除数量 : "+String.valueOf(count));
    }

    @RequestMapping(value = "update",method = RequestMethod.PUT)
    public R update(@RequestBody SysUser sysUser){
        System.out.println("更新 =》"+sysUser);
        int count = userService.update(sysUser);
        return R.ok().message("更新数量 : "+String.valueOf(count));
    }

    @RequestMapping("test")
    public Map test( @RequestParam("len") Integer len){
//        String result = "{ \"text\": \"<span class=\\\"ql-align-center\\\"><span style=\\\"color: rgb(255, 255, 255);\\\">富文本编辑器我是动态的</span></span>\" }";

        Map map = new HashMap();
        String result = "<span class=\"ql-align-center\"><span style=\"color: rgb(255, 255, 255);\">富文本编辑器我是动态的</span></span>";
        if(len>10){
            result = result.replace("rgb(255, 255, 255);","red;");
        }
        map.put("test",result);
        return map;
    }

}
